#include "../include/admin_handler.h"
#include "../include/db_manager.h"
#include <jsoncpp/json/json.h>
#include <iostream>

std::string AdminHandler::getSystemStatistics() {
    DBManager& db = DBManager::getInstance();
    
    Json::Value response;
    response["success"] = true;
    response["statistics"] = Json::Value(Json::objectValue);
    
    // 员工总数
    auto emp_results = db.executeSelectQuery("SELECT COUNT(*) as total FROM employees");
    int total_employees = emp_results.empty() ? 0 : std::stoi(emp_results[0].at("total"));
    
    // 部门总数
    auto dept_results = db.executeSelectQuery("SELECT COUNT(*) as total FROM departments");
    int total_departments = dept_results.empty() ? 0 : std::stoi(dept_results[0].at("total"));
    
    // 工资记录总数
    auto salary_results = db.executeSelectQuery("SELECT COUNT(*) as total FROM salary");
    int total_salary_records = salary_results.empty() ? 0 : std::stoi(salary_results[0].at("total"));
    
    // 考勤记录总数
    auto attendance_results = db.executeSelectQuery("SELECT COUNT(*) as total FROM attendance");
    int total_attendance_records = attendance_results.empty() ? 0 : std::stoi(attendance_results[0].at("total"));
    
    response["statistics"]["total_employees"] = total_employees;
    response["statistics"]["total_departments"] = total_departments;
    response["statistics"]["total_salary_records"] = total_salary_records;
    response["statistics"]["total_attendance_records"] = total_attendance_records;
    
    Json::FastWriter writer;
    return writer.write(response);
}

std::string AdminHandler::getSystemReports() {
    DBManager& db = DBManager::getInstance();
    
    Json::Value response;
    response["success"] = true;
    response["reports"] = Json::Value(Json::objectValue);
    
    // 部门员工统计
    auto dept_emp_results = db.executeSelectQuery("SELECT d.Dname, COUNT(e.Employee_id) as employee_count FROM departments d LEFT JOIN employees e ON d.Dno = e.Dno GROUP BY d.Dno, d.Dname");
    
    Json::Value dept_report = Json::Value(Json::arrayValue);
    for (const auto& row : dept_emp_results) {
        Json::Value dept;
        dept["department_name"] = row.at("Dname");
        dept["employee_count"] = std::stoi(row.at("employee_count"));
        dept_report.append(dept);
    }
    response["reports"]["department_employee_stats"] = dept_report;
    
    // 工资发放统计
    auto salary_results = db.executeSelectQuery("SELECT Year, Month, COUNT(*) as employee_count, SUM(Shifa_pay) as total_paid FROM salary GROUP BY Year, Month ORDER BY Year DESC, Month DESC LIMIT 12");
    
    Json::Value salary_report = Json::Value(Json::arrayValue);
    for (const auto& row : salary_results) {
        Json::Value salary;
        salary["year"] = row.at("Year");
        salary["month"] = row.at("Month");
        salary["employee_count"] = std::stoi(row.at("employee_count"));
        salary["total_paid"] = row.at("total_paid");
        salary_report.append(salary);
    }
    response["reports"]["salary_stats"] = salary_report;
    
    Json::FastWriter writer;
    return writer.write(response);
}

std::string AdminHandler::getSystemInfo() {
    Json::Value response;
    response["success"] = true;
    response["system_info"] = Json::Value(Json::objectValue);
    
    response["system_info"]["version"] = "1.0.0";
    response["system_info"]["name"] = "薪资管理系统";
    response["system_info"]["description"] = "基于C++和MySQL的现代化薪资管理系统";
    response["system_info"]["author"] = "System Developer";
    response["system_info"]["database"] = "MySQL";
    response["system_info"]["framework"] = "cpp-httplib";
    
    Json::FastWriter writer;
    return writer.write(response);
}

std::string AdminHandler::backupDatabase() {
    // 简化实现
    Json::Value response;
    response["success"] = true;
    response["message"] = "数据库备份功能待实现";
    
    Json::FastWriter writer;
    return writer.write(response);
}

std::string AdminHandler::restoreDatabase() {
    // 简化实现
    Json::Value response;
    response["success"] = true;
    response["message"] = "数据库恢复功能待实现";
    
    Json::FastWriter writer;
    return writer.write(response);
} 